package com.wsq.activityliftcycle

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import kotlinx.android.synthetic.main.activity_aactivity.*

class AActivity : AppCompatActivity() {

    companion object {
        @JvmStatic
        var index: Int = 0
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_aactivity)
        var transient = supportFragmentManager.beginTransaction()
        
        lifecycle.addObserver(object : LifecycleEventObserver {
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                Log.d("AActivity:", "Event:" + event.name)
                if (event == Lifecycle.Event.ON_STOP){
                    index--
                    Log.d("AActivity:", "Event:index=" + index)
                }
            }
        })

        skipSelf.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                var aIntent = Intent(this@AActivity, AActivity::class.java)
                startActivity(aIntent)
            }
        })
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        index++;
        Log.d("AActivity:", "Event:onNewIntent")
        Log.d("AActivity:", "Event:index=" + index)
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.d("AActivity:", "Event:onSaveInstanceState" )
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d("AActivity:", "Event:onRestoreInstanceState" )
    }
}